﻿using Gameplay.PVE;
using NpcFramework;
using System;
using System.Collections.Generic;
using UnityEngine;

public class PveNpcViewLayer : NpcViewLayer,
    INpcAnimation
{
    
    private Dictionary<int,PveNpcViewInfo> NpcViewInfos = new Dictionary<int, PveNpcViewInfo>();
    private Dictionary<int, string> npcPlayingAnim = new Dictionary<int, string>();
    #region NPC增删

    public void AddNpcViewInfo(int npcId, PveNpcViewInfo viewInfo)
    {
        if (NpcViewInfos.ContainsKey(npcId))
        {
            return;
        }
        NpcViewInfos.Add(npcId,viewInfo);
        npcPlayingAnim.Add(npcId,string.Empty);
    }

    public void RemoveNpcViewInfo(int npcId)
    {
        if (NpcViewInfos.ContainsKey(npcId))
        {
            NpcViewInfos.Remove(npcId);
            npcPlayingAnim.Remove(npcId);
        }
    }
    

    #endregion
    
    #region 辅助方法

    private Transform GetNpcTransform(int npcId)
    {
        PveNpcViewInfo info = null;
        if (NpcViewInfos.TryGetValue(npcId,out info))
        {
            return info.Transform;
        }

        return null;
    }
    
    private Animator GetNpcAnimator(int npcId)
    {
        PveNpcViewInfo info = null;
        if (NpcViewInfos.TryGetValue(npcId,out info))
        {
            return info.Animator;
        }

        return null;
    }

    #endregion


    #region 接口实现

    public override void SetPosition(int npcId, Vector3 pos)
    {
        Transform transform = GetNpcTransform(npcId);
        Animator animator = GetNpcAnimator(npcId);
        if (animator)
        {
            // TODO Locomotion
        }
        
        if (transform) transform.position = pos;
    }

    public override void SetRotation(int npcId, Quaternion rot)
    {
        Transform transform = GetNpcTransform(npcId);
        if (transform) transform.rotation = rot;
    }

  
    public void PlayAnimation(int npcId,string stateName, int layer = -1, float normalizedTime = Single.NegativeInfinity)
    {
        Animator animator = GetNpcAnimator(npcId);
        if (animator == null)
        {
            return;
        }

        if (npcPlayingAnim[npcId] == stateName)
        {
            return;
        }
        else
        {
            npcPlayingAnim[npcId] = stateName;
        }

        foreach (var param in animator.parameters)
        {
            animator.ResetTrigger(param.name);
        }
        var itemController = animator.transform.GetComponent<NpcItemController>();
        if (stateName == NPCAnim.walk.ToString())
        {
            animator.SetFloat("WalkH", 0);
            animator.SetFloat("WalkV", 1);
            
            if (itemController!=null)
            {
                itemController.HideAllItemPoint();
            }
        }
        animator.SetTrigger(stateName);
        if (itemController != null)
        {
            if (stateName == NPCAnim.phone.ToString() || stateName == NPCAnim.phone_talk.ToString() || stateName == NPCAnim.look_phone.ToString())
            {
                itemController.SetItemPointActive(true, "phone");
            }
            else if (stateName == NPCAnim.smoke.ToString())
            {
                itemController.SetItemPointActive(true, "smoke");
            }
        }
        //if (stateName == "Run")
        //{
        //    animator.SetTrigger("Walk");
        //    if (npcId == -666)
        //    {
        //        animator.SetFloat("WalkH", 0);
        //        animator.SetFloat("WalkV", 1);
        //    }
        //    else
        //    {
        //        animator.speed = Random.Range(1, 1.1f);
        //        animator.ResetTrigger("idle");
        //        animator.SetTrigger("walk");
        //    }
        //}
        //else
        //{
        //    if (npcId != -666)
        //    {
               
        //    }
        //    else
        //    {
        //        foreach (var param in animator.parameters)
        //        {
        //            animator.ResetTrigger(param.name);
        //        }
        //        animator.SetBool("Walk", false);
        //        animator.SetTrigger(stateName);
        //    }
        //}
       
        //GetNpcAnimator(npcId)?.Play(stateName,layer,normalizedTime);
    }

    #endregion

    /// <summary>
    /// 清楚某个npc的正在播放动画的状态
    /// </summary>
    /// <param name="npcId"></param>
    public void ClearNpcAnimState(int npcId)
    {
        if (npcPlayingAnim == null)
        {
            return;
        }
        if (npcPlayingAnim.ContainsKey(npcId))
        {
            npcPlayingAnim[npcId] = string.Empty;
        }
    }
    public void ClearPlayingAnimState()
    {
        npcPlayingAnim?.Clear();
    }

    public override void Dispose()
    {
        if (NpcViewInfos != null)
        {
            NpcViewInfos.Clear();
            NpcViewInfos = null;
        }

        if (npcPlayingAnim != null)
        {
            npcPlayingAnim.Clear();
            npcPlayingAnim = null;
        }

    }
}
